package org.mokusakura.web.controller;

import org.mokusakura.constant.RequestAttributeKeyConstant;
import org.mokusakura.constant.ServletUrlConstant;
import org.mokusakura.constant.SessionAttributeKeyConstant;
import org.mokusakura.domain.*;
import org.mokusakura.service.CatalogService;
import org.mokusakura.service.OrderService;
import org.mokusakura.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@Controller
public class DispatcherController {
    public static final String PARAMETER_KEY_CATEGORY_ID_TO_SHOW = "categoryID";
    public static final String ITEM_ID_TO_SHOW = "itemID";
    public static final String PARAMETER_KEY_ORDER_ID = "orderID";
    public static final String PARAMETER_KEY_PRODUCT_ID = "productID";
    public static final String KEY_WORD_TO_SEARCH = "keyword";
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @RequestMapping("/loginphone")
    public String forwardToLoginPhone() {
        return "account/login-phone";
    }

    @RequestMapping(ServletUrlConstant.ACCOUNT_INFORMATION_SERVLET_URL)
    public String forwardToAccountInformation(@SessionAttribute(SessionAttributeKeyConstant.LOGIN_USER) User user) {
        return "account/accountInformation";
    }

    @RequestMapping(ServletUrlConstant.CART_PAGE_SERVLET_URL)
    public String forwardToCartPage(
            @SessionAttribute(value = SessionAttributeKeyConstant.LOGIN_USER, required = false) User user) {
        if (user == null) {
            return "account/login";
        } else {
            Cart userCart = userService.getUserCart(user);
            user.setCart(userCart);
            return "cart/cart";
        }
    }

    @RequestMapping(ServletUrlConstant.CATEGORY_SERVLET_URL)
    public String forwardToCategoryPage(Model model,
                                        @RequestParam(PARAMETER_KEY_CATEGORY_ID_TO_SHOW) String categoryId) {
        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        model.addAttribute(RequestAttributeKeyConstant.CATEGORY_ID_TO_SHOW, category);
        model.addAttribute(RequestAttributeKeyConstant.PRODUCT_LIST_TO_SHOW, productList);
        return "catalog/category";
    }

    @RequestMapping(ServletUrlConstant.ITEM_SERVLET_URL)
    public String forwardToItemPage(Model model, @RequestParam(ITEM_ID_TO_SHOW) String itemId) {

        Item item = catalogService.getItem(itemId);
        model.addAttribute(SessionAttributeKeyConstant.ITEM_TO_SHOW, item);
        return "catalog/item";
    }

    @RequestMapping(ServletUrlConstant.LOGIN_PAGE_SERVLET_URL)
    public String forwardToLoginPage(
            @SessionAttribute(value = SessionAttributeKeyConstant.LOGIN_USER, required = false) User user) {
        return user == null ? "account/login" : "catalog/main";
    }

    @RequestMapping(ServletUrlConstant.MAIN_SERVLET_URL)
    public String forwardToMainPage() {
        return "catalog/main";
    }


    @RequestMapping(ServletUrlConstant.ORDER_DETAIL_SERVLET_URL)
    public String forwardToOrderDetailPage(Model model, @RequestParam(PARAMETER_KEY_ORDER_ID) Long orderId,
                                           @SessionAttribute(SessionAttributeKeyConstant.LOGIN_USER) User user) {
        Order detailOrder = orderService.getOrderByOrderId(orderId);
        if (detailOrder.getUserId().equals(user.getId())) {
            model.addAttribute(RequestAttributeKeyConstant.ORDER_TO_SHOW, detailOrder);
            return "order/orderDetail";
        } else {
            return "catalog/main";
        }
    }

    @RequestMapping(ServletUrlConstant.ORDER_LIST_PAGE_SERVLET_URL)
    public String forwardToOrderListPage(Model model,
                                         @SessionAttribute(SessionAttributeKeyConstant.LOGIN_USER) User user) {
        List<Order> orderList = orderService.getOrderListByUser(user);
        model.addAttribute(RequestAttributeKeyConstant.ORDER_LIST_TO_SHOW, orderList);
        return "order/orderList";
    }

    @RequestMapping(ServletUrlConstant.PRODUCT_SERVLET_URL)
    public String forwardToProductPage(Model model, @RequestParam(PARAMETER_KEY_PRODUCT_ID) String productId) {
        List<Item> itemList = catalogService.getItemListByProduct(productId);
        Product product = catalogService.getProduct(productId);
        if (product == null) {
            return "error";
        }
        model.addAttribute(RequestAttributeKeyConstant.PRODUCT_TO_SHOW, product);
        model.addAttribute(RequestAttributeKeyConstant.ITEM_LIST_TO_SHOW, itemList);
        return "catalog/product";
    }

    @RequestMapping(ServletUrlConstant.REGISTER_PAGE_SERVLET_URL)
    public String forwardToRegisterPage(
            @SessionAttribute(value = SessionAttributeKeyConstant.LOGIN_USER, required = false) User user) {
        return user == null ? "account/register" : "catalog/main";
    }

    @PostMapping(ServletUrlConstant.SEARCH_PAGE_SERVLET_URL)
    public String forwardToSearchPage(Model model, @RequestParam(KEY_WORD_TO_SEARCH) String keyword) {
        List<Product> productList = catalogService.searchProductByKeyword(keyword);
        model.addAttribute(RequestAttributeKeyConstant.PRODUCT_LIST_TO_SHOW, productList);
        return "catalog/searchProduct";
    }


}
